package com.bodu.depress.service.impl;

import com.bodu.core.entity.enums.StatusFlagEnum;
import com.bodu.depress.entity.request.DepressOptionsRequest;
import com.bodu.depress.entity.vo.DepressOptionsVo;
import com.bodu.depress.service.DepressOptionsService;
import com.bodu.core.entity.ParamReq;
import com.bodu.depress.mapper.DepressOptionsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目选项 Service impl
 *
 * @author Administrator
 */
@Service
public class DepressOptionsServiceImpl implements DepressOptionsService {

    @Resource
    private DepressOptionsMapper depressOptionsMapper;

    @Override
    public DepressOptionsVo getModel(Long id) {
        DepressOptionsVo depressOptionsVo = depressOptionsMapper.getModel(id);
        depressOptionsVo.setStatusFlagName(StatusFlagEnum.getStatusValues(depressOptionsVo.getStatusFlag()));
        return depressOptionsVo;
    }

    @Transactional
    @Override
    public Long save(DepressOptionsRequest request) {
        depressOptionsMapper.save(request);
        return request.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long update(DepressOptionsRequest request) {
        depressOptionsMapper.update(request);
        return request.getId();
    }

    @Override
    public List<DepressOptionsVo> listPage(ParamReq<DepressOptionsRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<DepressOptionsVo> res = depressOptionsMapper.listPage(params);
        res.forEach(r -> r.setStatusFlagName(StatusFlagEnum.getStatusValues(r.getStatusFlag())));
        return res;
    }

    @Override
    public Integer countPage(ParamReq<DepressOptionsRequest> params) {
        return depressOptionsMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return depressOptionsMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public List<DepressOptionsVo> findOptionsByQuestionId(Long id) {
        return depressOptionsMapper.findOptionsByQuestionId(id);
    }
}
